package com.gy.componit;


import com.gy.event.FireEvent;
import com.gy.game.Game;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.math.BigDecimal;

/**
 * 炮台
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Battery implements Component {

    // 位置
    private BigDecimal x,y;
    // 所属队伍
    private Team team;
    // 起始角度 度
    private Integer startAngle;
    // 偏转幅度 度
    private Integer diffAngle = 50;
    // 摆动速度
    private double v = 1;
    // 大小
    private int size = 10;
    // 发射冷却
    private int cd = 100;
    // 默认冷却时间
    private int defaultCd = 5;
    // 子弹数
    private int bullet = 4000;
    // 当前朝向角度
    private double currDirection;

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillOval(x.intValue()-size/2-2, y.intValue()-size/2-2, size + 4, size + 4);
        g.setColor(team.getBatteryColor());
        g.fillOval(x.intValue()-size/2, y.intValue()-size/2, size, size);
        // 画炮头
        g.setColor(team.getBatteryColor().darker());

        int i = (int)(currDirection);
        g.fillArc(x.intValue()-size*2, y.intValue()-size*2, size * 4, size * 4, i-10, 20);
    }

    @Override
    public AreaPoint getAreaPoint(int areaSize) {
        return new AreaPoint(x.intValue()/areaSize, y.intValue()/areaSize);
    }

    // 当前角度计数
    private double angleNum = 0;
    private int maxNum = 400;

    @Override
    public void move(Game game) {
        currDirection = fMove(angleNum)*diffAngle + startAngle;
        angleNum+=v;
        if(angleNum >= maxNum) angleNum = 0;
        cd--;
        if(cd <= 0 && bullet > 0){
            fire(game);
            cd = defaultCd;
            bullet -= 1;
        }
    }

    private double fMove(double x) {
        x/=100;
        if(x<=1) return x-1;
        else if(x<=2) return x-1;
        else if(x<=3) return 3-x;
        else return 3-x;
    }

    private void fire(Game game) {
        FireEvent event = new FireEvent();
        event.setBattery(this);
        game.addEvent(event);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
